package cn.newtouch.org.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.util.ExcelUtils;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.org.domain.Shop;
import cn.newtouch.org.domain.ShopLog;
import cn.newtouch.org.dto.ShopDto;
import cn.newtouch.org.query.ShopQuery;
import cn.newtouch.org.service.IShopService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API",description="店铺相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有店铺" )
    //@PreAuthorize(name = "查询所有店铺",sn = "shop:getAll")
    public List<Shop> findAll(){
        return shopService.loadAll();
    }

    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询店铺",sn = "shop:get")
    public Shop findOne(@PathVariable Long id){
       return shopService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "通过ID删除店铺",sn = "shop:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存店铺",sn = "shop:save")
    public JsonResult updateOrAdd(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                //添加操作
                shopService.save(shop);
            }else{
                //修改操作
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    //@PreAuthorize(name = "分页查询店铺",sn = "shop:list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            return shopService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除店铺",sn = "shop:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 商家入驻
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){

        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) {
            //业务异常
            return JsonResult.me().setMsg("入驻失败!"+e.getMessage());
        }catch (Exception e) {
            //代码问题
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试！");
        }
    }

    /**
     * 审核通过
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败！");
        }
    }

    /**
     * 驳回
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回" )
    public JsonResult reject(@RequestBody ShopLog log){
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败！");
        }
    }

    /**
     * 导出Excel表
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            //查询所以shop
            List<Shop> list = shopService.loadAll();
            //调用工具类List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
            //  HttpServletResponse response
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入Excel表
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 报表
     * domain：跟数据库中表对应
     * query：查询使用
     * dto：接收参数或者传参使用
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){

        return shopService.getEcharts();
    }




}
